<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/5
 * Time: 下午5:52
 */

namespace App\Busi\Modules\Resource;


use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\CategoryTrait;
use App\Busi\Traits\CompanyTrait;

use App\Busi\Traits\UserTrait;
use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Http\Requests\UserInfoRo;

class UserInfoBusi extends BaseBusi
{
    use CompanyTrait,CategoryTrait,UserTrait;
    public function handle(UserInfoRo $ro){
        $uid = $ro->uid;
        $user = $this->getUserByUid($uid);
        if(!$user){
            throw new UserException(UserError::USER_NOT_EXIST);
        }
        $companyinfo = $this->getCompanyByUid($uid);
        if($companyinfo == null){
            $securities['name'] ="";
            $securities['province'] ="";
            $securities['city'] ="";
            $securities['address'] ="";
            $securities['scale'] ="";
            $securities['scale'] ="";
            $securities['licence_poster'] = "";
            $company['desc'] = "";
            $company['advantage'] = "";
            $company['categorys'] = "";
            $company['logo'] = "";
            $company['ablum'] = "";
            $company['contact_name'] = "";
            $company['contact_number'] = "";
            $ret = [
                'securities'=>$securities,
                'company'=>$company

            ];
            return $ret;
        }

        $securities = [];
        $company = [];
        unset($companyinfo['account']);
        $securities['name'] =$companyinfo['name'];
        $securities['province'] =$this->getDistNameid($companyinfo['province_id']);
        $securities['city'] =$this->getDistNameid($companyinfo['city_id']);
        $securities['address'] =$companyinfo['address'];
        $securities['scale'] =$companyinfo['scale'];
        $securities['licence_poster'] =config('admin.upload.host').$companyinfo['licence_poster'];
        $securities['verify_status'] = $companyinfo['flag'];
        $company['desc'] = $companyinfo['desc'];
        $company['advantage'] = $companyinfo['advantage'];
        $categoryinfo = $companyinfo->subcategorys;
        $categorys = [];
        foreach ($categoryinfo as $category){
            $category_item = [];
            $category_item['name'] = $this->getFullCategoryNameByID($category['id']);
            $category_item['id'] = $category['id'];
            array_push($categorys,$category_item);
        }
        $company['categorys'] = $categorys;
        if($companyinfo['logo']){
            $company['logo'] = config('admin.upload.host').$companyinfo['logo'];
        }else{
            $company['logo'] = "";
        }


        $ablum = $companyinfo['pictures'];
        if($ablum){
            foreach ($ablum as &$item){
                $item = config('admin.upload.host').$item;
            }
        }

        $company['ablum'] = $ablum;
        $company['contact_name'] = $companyinfo['contact_name'];
        $company['contact_number'] = $companyinfo['contact_number'];
        $ret = [
            'securities'=>$securities,
            'company'=>$company

        ];
        return $ret;

    }
}